package com.miao.shop.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Operation;
import com.miao.common.entity.pojo.Refund;
import com.miao.shop.service.ShopOperationService;
import com.miao.shop.service.ShopRefundService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 退货记录表(Refund)表控制层
 *
 * @author MiaoWenQing
 * @since 2021-02-23 00:19:07
 */
@RestController
@RequestMapping("common/refund")
public class ShopRefundController {
    /**
     * 服务对象
     */
    @Resource
    private ShopRefundService shopRefundService;

    @Resource
    private ShopOperationService shopOperationService;
    
    
    

    @PostMapping("/apply")
    public R refundApply(@RequestBody Refund refund){
        refund.setRefAppliyTime(new Date());
        if (this.shopRefundService.save(refund)) {

            Operation operation = new Operation();
            Map map = JSON.parseObject(refund.getProId(), Map.class);


            operation.setOperationDes("顾客因"+refund.getRefReason()+"对商品"+map.get("proName")+"提出售后服务");
            operation.setOperationTime(new Date());
            operation.setOrderId(refund.getOrderId());
            operation.setRefId(refund.getRefId());
            this.shopOperationService.save(operation);
            return R.ok();
        }
        return R.error();

    }


    /**
     * 根据用户id获取全部服务单的申请
     * @param cusId 客户id
     * @return R
     */
    @GetMapping("/get")
    public R refundApplyList(String cusId){

        QueryWrapper<Refund> refundQueryWrapper = new QueryWrapper<>();
        refundQueryWrapper.eq("cus_id",cusId);
        List<Refund> list = this.shopRefundService.list(refundQueryWrapper);
        return R.ok(list);


    }


    /**
     * 获取操作记录
     * @param refId 服务单号
     * @return R
     */
    @GetMapping("/option")
    public R optionRef(String refId){
        QueryWrapper<Operation> operationQueryWrapper = new QueryWrapper<>();
        operationQueryWrapper.eq("ref_id" ,refId);
        List<Operation> list = this.shopOperationService.list(operationQueryWrapper);
        return R.ok(list);
    }


}
